<html>
<head><meta charset="utf-8"><title>Adding methods as more specific versions of `as` · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html">Adding methods as more specific versions of `as`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="238370887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238370887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238370887">(May 11 2021 at 19:44)</a>:</h4>
<p>In the <span class="user-group-mention" data-user-group-id="1977">@T-lang</span> meeting today, we were discussing error-prone usages of <code>as</code> and potentially linting against them. In that meeting, the idea came up of adding library methods that let people more precisely specify conversions they want; for instance, <code>.truncate()</code> (only from integer to smaller integer), or something to convert types of the same size only (<code>usize</code> to <code>u64</code> and vice versa on 64-bit but not 32-bit), etc.</p>



<a name="238370939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238370939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238370939">(May 11 2021 at 19:44)</a>:</h4>
<p><span class="user-mention" data-user-id="310399">@Mara</span> and I were both in the meeting, and I wanted to raise the topic for libs discussion (and potentially for discussion in a meeting later).</p>



<a name="238374326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238374326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238374326">(May 11 2021 at 20:09)</a>:</h4>
<p>Is this linting against all uses of <code>as</code>, or just specific ones? There are cases where <code>... as _</code> can fix type checking by forcing the compiler to look elsewhere to infer the specific details of the type</p>



<a name="238374585"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238374585" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238374585">(May 11 2021 at 20:11)</a>:</h4>
<p>Linting against integer -&gt; integer conversions or pointer -&gt; integer conversions seems reasonable though</p>



<a name="238374856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238374856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238374856">(May 11 2021 at 20:13)</a>:</h4>
<p>If type ascription ever lands then <code>...: _</code> would be one way to indicate a coercion without the problems of <code>as</code></p>



<a name="238379738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238379738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238379738">(May 11 2021 at 20:48)</a>:</h4>
<p>The version I was thinking is <code>to_bits: *T -&gt; usize</code> and <code>from_bits: usize -&gt; *T</code>.</p>



<a name="238379774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238379774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238379774">(May 11 2021 at 20:48)</a>:</h4>
<p>To have a way to show the desire to switch "kinds", in a way.</p>



<a name="238379977"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238379977" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238379977">(May 11 2021 at 20:50)</a>:</h4>
<p>(Like how there's <code>.cast::&lt;T&gt;()</code> to say that you're trying to stay as the same kind of pointer.)</p>



<a name="238380107"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238380107" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238380107">(May 11 2021 at 20:51)</a>:</h4>
<p>it would be useful to have the lang team weigh in on what role they think <code>as</code> should play in an ideal version of Rust. is the idea that anything that <code>as</code> does today that could possibly be done by a function, should instead be done by a function?</p>



<a name="238383709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238383709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238383709">(May 11 2021 at 21:17)</a>:</h4>
<blockquote>
<p>useful to have the lang team weigh in</p>
</blockquote>
<p>well the lang team discussed it today and wanted input from the libs team on this ^^'</p>



<a name="238386174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238386174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238386174">(May 11 2021 at 21:39)</a>:</h4>
<p>right, I'm asking for more philosophical guidance. :) is the goal to <em>completely obviate</em> <code>as</code>?</p>
<p>looking at the exhaustive list of possible casts in the reference:</p>
<ol>
<li>num-&gt;num: could be done via try and try_from</li>
<li>C-like enum-&gt;int: could possibly be done similarly to mem::discriminant. in some ways this would be worse than <code>as</code>, but in some ways it could be better (at least until <a href="https://github.com/rust-lang/rust/issues/81686#issuecomment-839182683">https://github.com/rust-lang/rust/issues/81686#issuecomment-839182683</a> is addressed)</li>
<li>bool/char-&gt;int: try/try_from</li>
<li>u8-&gt;char: try</li>
<li><code>*T</code>-&gt;<code>*U</code>: <code>pointer::cast</code></li>
<li><code>*T</code>-&gt;int: could be a function</li>
<li>int-&gt;<code>*T</code>: could be a function</li>
<li>array-&gt;<code>*T</code>: could be a method</li>
<li>fn item-&gt;fn ptr: hard to replace without magic</li>
<li>fn item-&gt;<code>*T</code>: hard to replace, but maybe just deprecate it?</li>
<li>fn item-&gt;int: should really just be deprecated</li>
<li>fn ptr-&gt;<code>*T</code>: hard to replace</li>
<li>fn ptr-&gt;int: hard to replace</li>
<li>closure-&gt;fn ptr: could be a function(?), but wouldn't be as good</li>
</ol>



<a name="238388570"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238388570" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238388570">(May 11 2021 at 22:02)</a>:</h4>
<p>I don't think we were quite ready to say "you should never ever need to use <code>as</code>", though I think it's possible some people may feel that way. (I'd have to think about it more myself.)</p>



<a name="238388609"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238388609" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238388609">(May 11 2021 at 22:03)</a>:</h4>
<p>But I do think there's a desire to start enumerating uses of <code>as</code> that we <em>could</em> very easily carve out a narrower function for and consider adding those functions.</p>



<a name="238388781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238388781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238388781">(May 11 2021 at 22:04)</a>:</h4>
<p>Also, I think for <code>num-&gt;num</code> there was a desire to be a little narrower than <code>from</code>. "widen" could be spelled <code>from</code>/<code>into</code>, but <code>truncate</code> seems potentially worthwhile even though <code>try_into</code> exists, and I'd <em>definitely</em> like a "same size only".</p>



<a name="238390295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238390295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238390295">(May 11 2021 at 22:19)</a>:</h4>
<p>sure, I tend to agree that you don't need to shoehorn every possible conversion into <code>Try</code> and that more specific conversions can result in more self-explanatory code. that list there is just me brainstorming what sort of things would be easy to turn into functions, and which would be hard</p>



<a name="238390575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238390575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238390575">(May 11 2021 at 22:22)</a>:</h4>
<p>cc <a href="https://github.com/rust-lang/rust/pull/81642#issuecomment-824518223">https://github.com/rust-lang/rust/pull/81642#issuecomment-824518223</a> for some conversation about a trait+derive for the "get me the discriminant with the best type" version.</p>
<p>Interestingly, that's in a way similar to the "give me the usize for the pointer" case -- <code>as</code> casting supports things other than <code>usize</code>, but casting a <code>repr(u64)</code> to a <code>u8</code> is probably often not what was wanted, just like casting a <code>*const T</code> to a a <code>u8</code> is probably not what was wanted...</p>



<a name="238390766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238390766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238390766">(May 11 2021 at 22:24)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60/near/238388781">said</a>:</p>
<blockquote>
<p>[...] but <code>truncate</code> seems potentially worthwhile even though <code>try_into</code> exists</p>
</blockquote>
<p>I'd still like to see <code>u8::wrapping_from(some_i32)</code>, which has been stalled in an RFC thread for about three years... <a href="https://github.com/rust-lang/rfcs/pull/2438#issuecomment-403255258">https://github.com/rust-lang/rfcs/pull/2438#issuecomment-403255258</a></p>



<a name="238391074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238391074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238391074">(May 11 2021 at 22:27)</a>:</h4>
<p>We honestly may just want to lint against <code>*const T -&gt; u8</code> casts now</p>



<a name="238391976"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238391976" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238391976">(May 11 2021 at 22:36)</a>:</h4>
<p><span class="user-mention silent" data-user-id="243558">Steven Fackler</span> <a href="#narrow/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60/near/238391074">said</a>:</p>
<blockquote>
<p>We honestly may just want to lint against <code>*const T -&gt; u8</code> casts now</p>
</blockquote>
<p>That's what started this conversation <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span>  <a href="https://github.com/rust-lang/rust/pull/81789">https://github.com/rust-lang/rust/pull/81789</a> is open that would do that, but the conversation about whether it would be reasonable include <code>*const Foo -&gt; u32</code> in that lint led to "well, it seems a bit strict for <code>rustc</code> if there isn't an alternative we could recommend instead".</p>



<a name="238392037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238392037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238392037">(May 11 2021 at 22:37)</a>:</h4>
<p>(As currently written in that PR it lints on things like <code>*const Foo -&gt; isize</code>, which definitely seems too aggressive.)</p>



<a name="238394091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238394091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BurntSushi <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238394091">(May 11 2021 at 22:57)</a>:</h4>
<p>i'm a bit behind here on what specifically is being proposed.</p>
<p>i think the case i care about most is what to do when i have a u8, u16, u32 or a u64, and i want to use it to index a slice. so in my code, i write a lot of <code>n as usize</code> and usually have a check somewhere _else_ that guarantees the conversion is correct by that point. is anything being proposed for that case?</p>
<p>(otherwise, i believe i generally agree with the broader principle of "lets use functions for things" instead of <code>as</code>. i think my only real concern with this is the ecosystem impact. <code>as</code> is used a lot, so whatever step we take here, unless it's particularly narrow, it's liable to cause churn. of course, i'm not saying that's a reason not to do it, but we should be cognizant of that.)</p>



<a name="238397353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238397353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238397353">(May 11 2021 at 23:28)</a>:</h4>
<p>for the indexing use case you could have <code>impl Index&lt;u8&gt; for Foo {</code>, <code>impl Index&lt;u16&gt; for Foo {</code>, <code>impl Index&lt;u32&gt; for Foo {</code>, <code>#[cfg(64_bit_arch)] impl Index&lt;u64&gt; for Foo {</code>, etc. People have been asking for this pain point to be relieved since forever, and that would be easier than doing any sort of numeric coercion. (Also, this might wreak havoc with type inference for all I know?)</p>



<a name="238398346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238398346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238398346">(May 11 2021 at 23:38)</a>:</h4>
<blockquote>
<p>(Also, this might wreak havoc with type inference for all I know?)</p>
</blockquote>
<p>It does, that's why it's not implemented already. Plenty of code does <code>let x = 2; use(a[x])</code> and this infers that <code>x</code> has type <code>usize</code> only because we know that <code>a</code> only has an impl for <code>usize</code>. Adding more impls would cause code like this to break or find the wrong type for <code>x</code></p>



<a name="238398602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238398602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238398602">(May 11 2021 at 23:41)</a>:</h4>
<p>It could be added at an edition boundary if introduced together with ascription, with autofix to replace <code>a[x]</code> with <code>a[x: usize]</code>.</p>



<a name="238399699"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20methods%20as%20more%20specific%20versions%20of%20%60as%60/near/238399699" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60.html#238399699">(May 11 2021 at 23:55)</a>:</h4>
<p><span class="user-mention silent" data-user-id="256342">bstrie</span> <a href="#narrow/stream/219381-t-libs/topic/Adding.20methods.20as.20more.20specific.20versions.20of.20.60as.60/near/238397353">said</a>:</p>
<blockquote>
<p>for the indexing use case you could have <code>impl Index&lt;u8&gt; for Foo {</code>, <code>impl Index&lt;u16&gt; for Foo {</code>, <code>impl Index&lt;u32&gt; for Foo {</code>, <code>#[cfg(64_bit_arch)] impl Index&lt;u64&gt; for Foo {</code>, etc. People have been asking for this pain point to be relieved since forever, and that would be easier than doing any sort of numeric coercion. (Also, this might wreak havoc with type inference for all I know?)</p>
</blockquote>
<p>I don't actually want the compiler to stop enforcing the use of <code>usize</code> for indexing. I just have some use cases where I'd like to be able to convert between <code>u64</code> and <code>usize</code> on a 64-bit system.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>